﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MinToTray
{ 
    public class AuthMgr
    {
        static Dictionary<IntPtr, IntPtr> hWnds = new Dictionary<IntPtr, IntPtr>();
        public static bool Auth(IntPtr hWnd)
        {
            if (hWnds.ContainsKey(hWnd))
            {
                //激活authform
                AuthForm form = AuthForm.FromHandle(hWnds[hWnd]) as AuthForm;
                form?.Activate();

                return false;
            }
            else
            {
                AuthForm form = new AuthForm();
                hWnds.Add(hWnd, form.Handle);
                
                try
                {
                    var result = form.ShowDialog(null);
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        return true;
                    }
                    return false;
                }
                finally
                {
                    hWnds.Remove(hWnd);
                }
            }
        }

        public static void CancelAuth(IntPtr hWnd)
        {
            if (hWnds.ContainsKey(hWnd))
            {
                try
                {
                    //激活authform
                    AuthForm form = AuthForm.FromHandle(hWnds[hWnd]) as AuthForm;
                    form?.Close();
                }
                finally
                {
                    hWnds.Remove(hWnd); 
                }
            }
        }

    }
}
